//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Research.DataLayer
{
    partial class Namespace
    {
        public bool HasParent
        {
            get
            {
                return (parent != null || parentID != Guid.Empty);
            }
        }

        public static List<Namespace> RootNodes(Connection c)
        {
            List<Namespace> results = new List<Namespace>();

            try
            {
                List<Namespace> allNodes = Namespace.LoadAll(c);

                foreach (Namespace item in allNodes)
                {
                    if (!item.HasParent)
                        results.Add(item);
                }
            }
            catch (RecordNotFoundException)
            {
                // No namespaces
            }

            return results;
        }

        public override void OnBeforeDelete()
        {
            base.OnBeforeDelete();

            if (this.Children.Count > 0)
                throw new ObjectInUseException("Namespace has inner nodes", null);
        }
    }
}
